package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 添加购物车(非查询类返回的值用不到)
     * @param shoppingCartdto
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartdto) {
        log.info("添加购物车数据为{}", shoppingCartdto);
        shoppingCartService.addCart(shoppingCartdto);
        return Result.success();
    }

    /**
     * 查询购物车数据
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> getAll() {
        List<ShoppingCart> result=shoppingCartService.showshoppingcart();
        //这里调用了我们写的动态查询(根据多条件实现模糊查询)
        return Result.success(result);
    }


    /**
     * 清空购物车
     * 我们非查询类返回不需要
     */
    @DeleteMapping("/clean")
    public Result clean() {
        shoppingCartService.cleanshoppingcart();
        return Result.success();
    }

    /**
     * 删除一个菜品(购物车中)
     * @param shoppingCartdto
     * @return
     */
    @PostMapping("/sub")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartdto) {
        shoppingCartService.decrease(shoppingCartdto);
        return Result.success();
    }

}
